﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.CommonBase.View.Controllers;
using Demo.CommonBase.View.Contracts;
using Demo.Services.Services.Impl.Managers;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;

namespace Demo.Services.View.Controllers
{
    public sealed class OperationController : AbstractDemoController
    {
        protected override void ConcreteExecute(IView view, string action)
        {
            try
            {
                switch (action)
                {
                    case "Services.GetOperations":
                        GetOperations(view);
                        break;
                    case "Services.GetProyectById":
                        GetProyectById(view);
                        break;
                    case "Services.GetResourceByProyect":
                        GetResourcesByProyect(view);
                        break;
                    case "Services.GetResourceLineTime":
                        GetResourcesLineTime(view);
                        break;
                    case "Services.SaveLineTime":
                        GetResourcesLineTime(view);
                        break;
                    case "Services.UpdateLineTime":
                        GetResourcesLineTime(view);
                        break; 
                    default: break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GetOperations(IView view)
        {
            OperationManager operationManager;
            OperationPagingInfo operationPaging;
            OperationFilter filter;
            try
            {
                Logger.Info();

                filter = (OperationFilter)view.GetValue("OperationFilter");
                operationManager = new OperationManager();
                operationPaging = operationManager.GetOperations(filter);
                view.SetValue("OperationPaging", operationPaging);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void GetProyectById(IView view)
        {
            ProyectManager proyectManager;
            ProyectInfo proyect;
            int id;
            try
            {
                Logger.Info();

                id = (int)view.GetValue("ProyectId");
                proyectManager = new ProyectManager();
                proyect = proyectManager.GetProyectById(id);
                view.SetValue("ProyectInfo", proyect);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void GetResourcesByProyect(IView view)
        {
            OperationManager operationManager;
            ResourcePagingInfo resource;
            ResourceFilter filter;            
            try
            {
                Logger.Info();

                filter = (ResourceFilter)view.GetValue("ResourceFilter");
                operationManager = new OperationManager();
                resource = operationManager.GetOperationResources(filter);
                view.SetValue("ResourcePagingInfo", resource);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void GetResourcesLineTime(IView view)
        {
            OperationManager operationManager;
            ResourceInfo resource;
            int id;
            try
            {
                Logger.Info();
                id = (int)view.GetValue("LineTimeId");
                operationManager = new OperationManager();
                resource = operationManager.GetResourceLineTime(id);
                view.SetValue("ResourceInfo", resource);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void SaveLineTime(IView view)
        {
            OperationManager operationManager;
            LineTimeInfo lineTime;            
            try
            {
                Logger.Info();
                lineTime = (LineTimeInfo)view.GetValue("LineTimeInfo");
                operationManager = new OperationManager();
                operationManager.SaveLineTime(lineTime);
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void UpdateLineTime(IView view)
        {
            OperationManager operationManager;
            LineTimeInfo lineTime;
            try
            {
                Logger.Info();
                lineTime = (LineTimeInfo)view.GetValue("LineTimeInfo");
                operationManager = new OperationManager();
                operationManager.UpdateLineTime(lineTime);

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

    }
}